package com.myBlog.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myBlog.pojo.Remark;
import com.myBlog.services.Remark.RemarkCaches;
import com.myBlog.services.Remark.RemarkService;
import com.myBlog.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class RemarkController {

    @Autowired
    private RemarkService remarkService;

    @Autowired
    private RemarkCaches remarkCaches;

    @RequestMapping("/user/remarks/query")
    public JSONObject getArticleListAuth(@RequestBody JSONObject params){
        JSONObject result = new JSONObject();

        // 评论暂无分页
        if(params.get("current")==null){
            params.put("current",1);
        }
        if(params.get("pageSize")==null){
            params.put("pageSize",10);
        }

        Page<Remark> pageData = remarkCaches.getCache(params.getInteger("current"), params.getInteger("pageSize"));
        result.put("success",true);
        result.put("current", params.getInteger("current"));
        result.put("data", pageData.getRecords());
        result.put("size",pageData.getSize());
        result.put("total",pageData.getTotal());

        return result;
    }

    @RequestMapping("/admin/remarks/query")
    public JSONObject getArticleList(@RequestBody JSONObject params){
        JSONObject result = new JSONObject();
        if(params.get("current")==null){
            params.put("current",1);
        }
        if(params.get("pageSize")==null){
            params.put("pageSize",10);
        }

        Page<Remark> page = new Page( params.getInteger("current"), params.getInteger("pageSize") );
        LambdaQueryWrapper<Remark> wrapper = new LambdaQueryWrapper<>();
        if(params.getInteger("remarkId")!=null){
            wrapper.eq(Remark::getRemarkId,params.getInteger("remarkId"));
        }
        if( !StringUtils.isEmpty(params.getString("content")) ){
            wrapper.eq(Remark::getContent,params.getString("content"));
        }
        if( !StringUtils.isEmpty(params.getString("email")) ){
            wrapper.eq(Remark::getEmail,params.getString("email"));
        }
        if( !StringUtils.isEmpty(params.getString("nickName")) ){
            wrapper.eq(Remark::getNickName,params.getString("nickName"));
        }
        if(params.getInteger("status") !=null){
            wrapper.eq(Remark::getStatus,params.getInteger("status"));
        }

        wrapper.orderByDesc(Remark::getRemarkTime);
        Page<Remark> pageData = remarkService.page(page,wrapper);

        result.put("success",true);
        result.put("data", pageData.getRecords());
        result.put("size",pageData.getSize());
        result.put("total",pageData.getTotal());

        return result;
    }


    @PostMapping("/admin/remarks/grant_on")
    public JSONObject grantOn(@RequestBody JSONObject params, HttpServletResponse response ){
        JSONObject resultMap = new JSONObject();
        List<Remark> list = params.getList("list",Remark.class);
        ArrayList<Remark> artilceList = new ArrayList<>();
        list.forEach(item->{
            Remark remark = new Remark();
            remark.setRemarkId(item.getRemarkId());
            remark.setStatus(1);
            artilceList.add(remark);
        });

        boolean updated = remarkService.updateBatchById(artilceList);
        if(updated){
            resultMap.put("success",true);
            resultMap.put("status",1);
            resultMap.put("info","操作成功");
        }else {
            resultMap.put("success",false);
            resultMap.put("status",0);
            resultMap.put("info","错误信息：数据库操作失败");
            response.setStatus(500);
        }

        return resultMap;
    }

    @PostMapping("/admin/remarks/grant_off")
    public JSONObject grantOff( @RequestBody JSONObject params,HttpServletResponse response ){
        JSONObject resultMap = new JSONObject();
        List<Remark> list = params.getList("list",Remark.class);
        ArrayList<Remark> artilceList = new ArrayList<>();
        list.forEach(item->{
            Remark remark = new Remark();
            remark.setRemarkId(item.getRemarkId());
            remark.setStatus(2);
            artilceList.add(remark);
        });

        boolean updated = remarkService.updateBatchById(artilceList);
        if(updated){
            resultMap.put("success",true);
            resultMap.put("status",1);
            resultMap.put("info","操作成功");
        }else {
            resultMap.put("success",false);
            resultMap.put("status",0);
            resultMap.put("info","错误信息：数据库操作失败");
            response.setStatus(500);
        }

        return resultMap;
    }

    @PostMapping("/admin/remarks/delete")
    public JSONObject delete( @RequestBody JSONObject params,HttpServletResponse response ){
        JSONObject resultMap = new JSONObject();
        resultMap.put("success",true);
        resultMap.put("status",1);
        resultMap.put("info","操作成功");
        List<Remark> list = params.getList("list",Remark.class);
        if(list.isEmpty()){
            return resultMap;
        }
        ArrayList<Remark> remarks = new ArrayList<>();
        list.forEach(item->{
            Remark remark = new Remark();
            remark.setRemarkId(item.getRemarkId());
            remarks.add(remark);
        });

        boolean removed = remarkService.removeBatchByIds(remarks);
        if(!removed){
            resultMap.put("success",false);
            resultMap.put("status",0);
            resultMap.put("info","错误信息：数据库操作失败");
            response.setStatus(500);
        }

        return resultMap;
    }


    @RequestMapping("/user/remarks/add")
    public JSONObject addRemark( @RequestBody Remark remark,HttpServletResponse response ){
        JSONObject resultMap = new JSONObject();
        // 状态统一为审核通过， 评论id抹掉
//      remark.setStatus(0);
        remark.setStatus(1);
        remark.setRemarkId(null);
        remarkCaches.appendToCache(remark);

        resultMap.put("success",true);
        resultMap.put("status",1);
        resultMap.put("info","操作成功");
        return resultMap;
    }

    // 缓存同步到数据库
    @PostMapping("/admin/remarks/refreshCatches")
    public JSONObject refreshCachesToDB(){
        remarkCaches.saveRemarksToDB();
        return JsonUtil.ok_info("缓存同步到数据库成功");
    }
}
